/**
 * Project Name:mauto-web
 * File Name:CommonUtils.java
 * Package Name:com.inftech.mauto.common.utils
 * Date:2016年11月1日下午7:00:35
 * Copyright (c) 2016, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.common.utils;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * ClassName:CommonUtils <br/>
 * Function: 共通工具类. <br/>
 * Date: 2016年11月1日 下午7:00:35 <br/>
 * 
 * @author puyf
 * @version
 * @since JDK 1.7
 */
public class CommonUtils {

	private static final String PATTERN1 = "yyyy-MM-dd";

	private static final String PATTERN2 = "yyyy-MM-dd HH:mm:ss";

	private static final String PATTERN3 = "yyyy-MM-dd HH:mm";

	private static final String PATTERN_YMD = "yyyyMMdd";

	// 地球平均半径
	private static final double EARTH_RADIUS = 6378137;

	/**
	 * 
	 * isNull:(判断对象是否为空). <br/>
	 *
	 * @author puyf
	 * @param object
	 * @return
	 * @since JDK 1.7
	 */
	public static boolean isBlank(Object object) {
		if (object == null || "".equals(object)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 
	 * getNowD:获取当前时间. <br/>
	 *
	 * @author puyf
	 * @return
	 * @since JDK 1.7
	 */
	public static Date getNowD() {
		return new Date();
	}

	/**
	 * 
	 * getNowL:获取当前时间. <br/>
	 *
	 * @author puyf
	 * @return
	 * @since JDK 1.7
	 */
	public static Long getNowL() {
		return new Date().getTime();
	}

	/**
	 * 
	 * getDate:获取某天yyyy-MM-dd. <br/>
	 *
	 * @author puyf
	 * @param offset
	 *            偏移值
	 * @return
	 * @since JDK 1.7
	 */
	public static String getDate(int offset) {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN1);
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.DAY_OF_MONTH, offset);
		String dateStr = sdf.format(ca.getTime());
		return dateStr;
	}

	/**
	 * 
	 * getThisYear:获取当前年. <br/>
	 *
	 * @author puyf
	 * @return
	 * @since JDK 1.7
	 */
	public static int getThisYear() {
		Calendar ca = Calendar.getInstance();
		return ca.get(Calendar.YEAR);
	}

	/**
	 * 
	 * getThisMonth:获取当前月. <br/>
	 *
	 * @author puyf
	 * @return
	 * @since JDK 1.7
	 */
	public static int getThisMonth() {
		Calendar ca = Calendar.getInstance();
		return ca.get(Calendar.MONTH) + 1;
	}

	/**
	 * 
	 * getNowStr:获取当天日期. <br/>
	 *
	 * @author puyf
	 * @return
	 * @since JDK 1.7
	 */
	public static String getTodayStr() {
		SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_YMD);
		String dateStr = sdf.format(new Date());
		return dateStr;
	}

	/**
	 * 
	 * formatDate1:日期格式化. <br/>
	 *
	 * @author puyf
	 * @param date
	 * @return
	 * @since JDK 1.7
	 */
	public static String formatDate1(Date date) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(PATTERN1);
			return sdf.format(date);
		} else {
			return null;
		}
	}

	/**
	 * 
	 * formatDate2:日期格式化. <br/>
	 *
	 * @author puyf
	 * @param date
	 * @return
	 * @since JDK 1.7
	 */
	public static String formatDate2(Date date) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(PATTERN2);
			return sdf.format(date);
		} else {
			return null;
		}
	}

	/**
	 * 
	 * formatDate3:日期格式化. <br/>
	 *
	 * @author puyf
	 * @param date
	 * @return
	 * @since JDK 1.7
	 */
	public static String formatDate3(Date date) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(PATTERN3);
			return sdf.format(date);
		} else {
			return null;
		}
	}

	/**
	 * 
	 * parseDate3:日期格式化. <br/>
	 *
	 * @author puyf
	 * @param date
	 * @return
	 * @since JDK 1.7
	 */
	public static Date parseDate3(String date) {
		if (!CommonUtils.isBlank(date)) {
			SimpleDateFormat sdf = new SimpleDateFormat(PATTERN3);
			try {
				return sdf.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * 
	 * getRandomInt:获取4位随机整数. <br/>
	 *
	 * @author puyf
	 * @return
	 * @since JDK 1.7
	 */
	public static int getRandomInt() {
		Random rand = new Random(System.currentTimeMillis());
		while (true) {
			int number = rand.nextInt(9999);
			if (number > 1000) {
				return number;
			}
		}
	}

	/**
	 * 
	 * moveFile:移动文件. <br/>
	 *
	 * @author puyf
	 * @param oldFile
	 * @param newPath
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public static void moveFile(File oldFile, String newPath) throws Exception {
		File newFlieDir = new File(newPath);
		if (!newFlieDir.exists()) {
			newFlieDir.mkdirs();
		}
		oldFile.renameTo(new File(newFlieDir, oldFile.getName()));
	}

	// 把经纬度转为度（°）
	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

	/**
	 * 根据两点间经纬度坐标（double值），计算两点间距离，单位为米
	 * 
	 * @param lng1
	 * @param lat1
	 * @param lng2
	 * @param lat2
	 * @return
	 */
	public static double getDistance(double lng1, double lat1, double lng2, double lat2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(
				Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;
	}
}
